package com.express.wechat.controller;

import com.express.bean.BootStrapTableExpress;
import com.express.bean.Express;
import com.express.bean.Message;
import com.express.mvc.ResponseBody;
import com.express.service.ExpressService;
import com.express.util.DateFormatUtil;
import com.express.util.JSONUtil;
import com.express.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @Author: laixiangdong
 * @Date 2020/12/09
 */
public class WechatExpressController {
    @ResponseBody("/wechat/findExpressByUserPhone.do")
    public String findExpressbyUserPhone(HttpServletRequest req, HttpServletResponse resp) {
        String userPhone = UserUtil.getWechatUserPhone(req.getSession());
        Message message = new Message();
        List<Express> list = ExpressService.findByUserPhone(userPhone);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e : list) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? "-" : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "待取件" : "已取件";
            // int id, String number, String userName, String userPhone, String company, String code, String inTime, String outTime, String status, String sysPhone
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), e.getCode(), inTime, outTime, status, e.getSysPhone());
            list2.add(e2);
        }
        if (list2.size() != 0) {
            Stream<BootStrapTableExpress> expressStream0 = list2.stream().filter(express -> {
                if ("待取件".equals(express.getStatus()))
                    return true;
                else
                    return false;
            }).sorted((o1, o2) -> {
                long time1 = DateFormatUtil.toTime(o1.getInTime());
                long time2 = DateFormatUtil.toTime(o2.getInTime());
                return (int) (time1 - time2);
            });
            Stream<BootStrapTableExpress> expressStream1 = list2.stream().filter(express -> {
                if ("已取件".equals(express.getStatus()))
                    return true;
                else
                    return false;
            }).sorted((o1, o2) -> {
                long time1 = DateFormatUtil.toTime(o1.getInTime());
                long time2 = DateFormatUtil.toTime(o2.getInTime());
                return (int) (time1 - time2);
            });
            Object[] s0 = expressStream0.toArray();
            Object[] s1 = expressStream1.toArray();
            message.setStatus(0);
            Map data = new HashMap<>();
            data.put("status0", s0);
            data.put("status1", s1);
            message.setData(data);

        } else {
            message.setStatus(-1);
        }
        return JSONUtil.toJSON(message.getData());
    }

    @ResponseBody("/wechat/userExpressList.do")
    public String expressList(HttpServletRequest req, HttpServletResponse resp) {
        String userPhone = req.getParameter("userPhone");
        List<Express> list = ExpressService.findByUserPhoneAndStatus(userPhone, 0);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e : list) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? "-" : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "待取件" : "已取件";
            // int id, String number, String userName, String userPhone, String company, String code, String inTime, String outTime, String status, String sysPhone
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), e.getCode(), inTime, outTime, status, e.getSysPhone());
            list2.add(e2);
        }
        Message message = new Message();
        if (list2.size() == 0) {
            message.setStatus(-1);
            message.setResult("未查询到");
        } else {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(list2);
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest req, HttpServletResponse resp) {
        String code = req.getParameter("code");
        Boolean flag = ExpressService.updateStatus(code);
        Message message = new Message();
        if (flag) {
            message.setStatus(0);
            message.setResult("取件成功");
        } else {
            message.setStatus(-1);
            message.setResult("取件失败，请重试");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/findExpressByCode")
    public String findExpressByCode(HttpServletRequest req, HttpServletResponse resp) {
        Express express = ExpressService.findByCode(req.getParameter("code"));
        Message message = null;
        if (express != null) {
            BootStrapTableExpress express2 = new BootStrapTableExpress();
            express2.setCode(express.getCode());
            express2.setCompany(express.getCompany());
            express2.setInTime(DateFormatUtil.format(express.getInTime()));
            express2.setNumber(express.getNumber());
            message = new Message(0, "查询成功", express2);
        } else {
            message = new Message(-1, "查询失败");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        // String number, String userName, String userPhone, String company, String sysPhone
        Express e = new Express(number, userName, userPhone, company, UserUtil.getWechatUserPhone(req.getSession()));
        Boolean flag = ExpressService.insert(e);
        Message message = new Message();
        if (flag) {
            message.setStatus(0);
            message.setResult("录入成功");
        } else {
            message.setStatus(-1);
            message.setResult("录入失败");
        }
        String s = JSONUtil.toJSON(message);
        return s;
    }
}
